import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {Router} from '@angular/router';
import {User} from '../../globalModel/User';
import {AdminService} from '../../service/admin.service';
import {LogService} from '../../service/log.service';
import {EmailService} from '../../service/email.service';
import {GoodsService} from '../../service/goods.service';
import {CoverService} from '../../service/cover.service';
import {UserService} from '../../service/user.service';

import {menus} from '../../globalModel/JsonLocalData';

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  encapsulation: ViewEncapsulation.None,
  styleUrls: ['./admin.component.css'],
  providers: [AdminService]
})
export class AdminComponent implements OnInit {
  width = 250; // 菜单栏宽度
  collapsed = false; // 默认展开菜单栏
  menus = menus; // 菜单栏数据
  selectMenu = null; // 当前点击的菜单栏
  user: User = new User(); // 用户数据对象
  /*
    全局变量
   */
  msg = null; // 全局提示消息
  progressValue = 0; // 全局进度条进度值
  componentName = null; // 当前子组件名称
  deleteDlgClosed = true; // true关闭删除弹框,false打开删除弹框
  deleteDlgBtnDisabled = false; // 默认开启删除弹框按钮（确认、删除）,true禁用
  deleteDlgTitle = null; // 删除弹框标题
  id = null; // 删除的id字符串

  /**
   * 构造函数
   */
  constructor(
    private router: Router,
    private adminService: AdminService,
    private logService: LogService,
    private emailService: EmailService,
    private goodsService: GoodsService,
    private coverService: CoverService,
    private userService: UserService) {
    /**
     * 监听子组件推送的消息
     */
    adminService.adminObservable.subscribe(adminVariable => {
      /**
       * 全局删除功能
       */
      if (adminVariable.deleteDlgClosed === false) {
        this.deleteDlgClosed = false;
        this.componentName = adminVariable.componentName;
        this.id = adminVariable.id;
        this.msg = this.id === 'all' ? `确定要清空所有数据` : '确定要删除所选数据';
        switch (this.componentName) {
          case 'log':
            this.deleteDlgTitle = '删除日志信息';
            break;
          case 'cover':
            this.deleteDlgTitle = '删除封面信息';
            break;
          case 'coverType':
            this.deleteDlgTitle = '删除封面类别信息';
            break;
          case 'goods':
            this.deleteDlgTitle = '删除商品信息';
            break;
          case 'goodsType':
            this.deleteDlgTitle = '删除商品类别信息';
            break;
          case 'email':
            this.deleteDlgTitle = '删除邮件信息';
            break;
          case 'user':
            this.deleteDlgTitle = '删除用户信息';
            break;
        }
      }
      /**
       * 全局进度条进度值
       */
      if (adminVariable.progressValue) {
        this.progressValue = adminVariable.progressValue;
        if (adminVariable.progressValue === 100) {
          setTimeout(() => {
            this.progressValue = 0;
          }, 800);
        }
      }
      // console.log('admin--');
      // console.log(adminVariable);
    });
  }

  /**
   * 初始化函数 state: 'open', selected: true,
   */
  ngOnInit() {
    this.user.name = sessionStorage.getItem('name'); // 用户名
    this.user.cover = sessionStorage.getItem('cover'); // 用户头像
  }

  /**
   * 隐藏、展开左侧菜单栏
   */
  toggle() {
    this.collapsed = !this.collapsed;
    this.width = this.collapsed ? 50 : 250;
  }

  /**
   * 点击左侧菜单栏
   * @param item
   */
  onItemClick(item) {
    // console.log('onItemClick');
    // console.log(item);
    // this.selectedMenu = item; // 当前选中的菜单栏
    // this.router.navigateByUrl(`admin/${item.link}`); // 跳转到指定组建
  }

  /**
   * 点击左侧菜单栏（点击同一菜单栏不会触发）
   * @param item
   */
  onSelectionChange(item) {
    // 跳转到组件
    this.router.navigateByUrl(`admin/${item.link}`);
  }

  /**
   * 退出登陆
   */
  onLogout(): void {
    sessionStorage.clear();
    this.router.navigateByUrl('login');
  }

  /**
   * 确认删除
   */
  onDeleteSure(): void {
    this.deleteDlgBtnDisabled = true; // 禁用弹框(确认、取消)按钮
    switch (this.componentName) {
      case 'log':
        this.logService.delete(this.id).subscribe(responseJson => {
          if (responseJson.code === 0) {
            this.msg = '删除成功！';
            this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: true});
            setTimeout(() => {
              this.reset();
            }, 800);
          } else {
            this.msg = '删除失败！';
            this.deleteDlgBtnDisabled = false; // 激活弹框(确认、取消)按钮
          }
        });
        break;
      case 'cover':
        this.coverService.deleteCover(this.id).subscribe(responseJson => {
          if (responseJson.code === 0) {
            this.msg = '删除成功！';
            this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: true});
            setTimeout(() => {
              this.reset();
            }, 800);
          } else {
            this.msg = '删除失败！';
            this.deleteDlgBtnDisabled = false; // 激活弹框(确认、取消)按钮
          }
        });
        break;
      case 'coverType':
        this.coverService.deleteCoverType(this.id).subscribe(responseJson => {
          if (responseJson.code === 0) {
            this.msg = '删除成功！';
            this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: true});
            setTimeout(() => {
              this.reset();
            }, 800);
          } else {
            this.msg = '删除失败！';
            this.deleteDlgBtnDisabled = false; // 激活弹框(确认、取消)按钮
          }
        });
        break;
      case 'goods':
        this.goodsService.deleteGoods(this.id).subscribe(responseJson => {
          if (responseJson.code === 0) {
            this.msg = '删除成功！';
            this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: true});
            setTimeout(() => {
              this.reset();
            }, 800);
          } else {
            this.msg = '删除失败！';
            this.deleteDlgBtnDisabled = false; // 激活弹框(确认、取消)按钮
          }
        });
        break;
      case 'goodsType':
        this.goodsService.deleteGoodsType(this.id).subscribe(responseJson => {
          if (responseJson.code === 0) {
            this.msg = '删除成功！';
            this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: true});
            setTimeout(() => {
              this.reset();
            }, 800);
          } else {
            this.msg = '删除失败！';
            this.deleteDlgBtnDisabled = false; // 激活弹框(确认、取消)按钮
          }
        });
        break;
      case 'emailCode':
        this.emailService.delete(this.id).subscribe(responseJson => {
          if (responseJson.code === 0) {
            this.msg = '删除成功！';
            this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: true});
            setTimeout(() => {
              this.reset();
            }, 800);
          } else {
            this.msg = '删除失败！';
            this.deleteDlgBtnDisabled = false; // 激活弹框(确认、取消)按钮
          }
        });
        break;
      case 'user':
        this.userService.delete(this.id).subscribe(responseJson => {
          if (responseJson.code === 0) {
            this.msg = '删除成功！';
            this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: true});
            setTimeout(() => {
              this.reset();
            }, 800);
          } else {
            this.msg = '删除失败！';
            this.deleteDlgBtnDisabled = false; // 激活弹框(确认、取消)按钮
          }
        });
        break;
    }

  }

  /**
   * 取消删除
   */
  onDeleteCancel(): void {
    this.deleteDlgClosed = true; // 关闭删除弹框
  }

  /**
   * 弹框关闭回调事件
   */
  onDlgClose(): void {
    this.reset(); // 重置变量
    this.adminService.pushAdminVariable({componentName: 'admin', deleteSuccess: false});
  }

  /**
   * 重置变量
   */
  reset(): void {
    this.msg = null; // 全局提示消息
    this.progressValue = 0; // 全局进度条进度值
    this.deleteDlgTitle = null;
    this.componentName = null;
    this.deleteDlgClosed = true; // 关闭删除弹框
    this.deleteDlgBtnDisabled = false; // 开启删除弹框按钮（确认、删除）
    this.id = null;
  }
}
